﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsAppSocket
{
    public partial class Form1 : Form
    {
        private Socket clientSocket;
        private byte[] receiveBuffer = new byte[1024];
        private StringBuilder messageBuilder = new StringBuilder();
        public Form1()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                // 创建客户端 Socket
                clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                // 获取服务器 IP 地址和端口号
                string serverIP = "192.168.30.185";
                int serverPort = 20000; // 替换为实际的端口号

                // 连接到服务器
                clientSocket.Connect(serverIP, serverPort);

                // 连接成功
                // AppendToOutputTextBox("Connected to the server.");
                MessageBox.Show("连接成功");
                // 启动接收消息的线程
                Thread receiveThread = new Thread(ReceiveMessages);
                receiveThread.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine("连接失败" + ex.Message);
                // 连接失败
                // AppendToOutputTextBox("Failed to connect to the server: " + ex.Message);
            }
        }
        private void ReceiveMessages()
        {
            try
            {
                while (true)
                {
                    // 接收消息
                    int bytesRead = clientSocket.Receive(receiveBuffer);
                    if (bytesRead > 0)
                    {
                        string message = Encoding.UTF8.GetString(receiveBuffer, 0, bytesRead);
                        messageBuilder.Append(message);

                        // 检查是否接收到完整的消息
                        if (message.EndsWith(Environment.NewLine))
                        {
                            // 显示接收到的消息
                            string receivedMessage = messageBuilder.ToString().TrimEnd();
                            MessageBox.Show("message" + receivedMessage);

                            // AppendToOutputTextBox("Received: " + receivedMessage);

                            // 清空消息构建器
                            messageBuilder.Clear();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // 处理接收消息时的异常
                // AppendToOutputTextBox("Error receiving messages: " + ex.Message);
            }
        }
        private void SendMessage(string message) {
            try
            {

                // 发送消息
                byte[] sendBuffer = Encoding.ASCII.GetBytes(message + Environment.NewLine);
                clientSocket.Send(sendBuffer);

                // 清空发送文本框
                // sendTextBox.Clear();

                // 显示发送的消息
                // AppendToOutputTextBox("Sent: " + message);
            }
            catch (Exception ex)
            {
                // 处理发送消息时的异常
                // AppendToOutputTextBox("Error sending message: " + ex.Message);
            }
        }
        private void btnSend_Click(object sender, EventArgs e)
        {
            SendMessage("AA7511020000CC");
        }
    }
}
